﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Security;

namespace BlobExplorer
{
    internal static class Utilities
    {
        public static string GetDelimitedList(this List<string> source)
        {
            string retval = "";

            for (int i = 0; i < source.Count; i++)
            {
                retval += source[i];
                if (i < source.Count - 1)
                {
                    retval += ",";
                }
            }

            return retval;
        }

        public static string GetContentType(string extension)
        {
            string mimeType = "application/unknown";

            try
            {
                RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(extension, false);

                if (regKey != null && regKey.GetValue("Content Type") != null)
                    mimeType = regKey.GetValue("Content Type").ToString();
                else
                {
                    switch (extension)
                    {
                        case ".xap":
                            mimeType = "application/x-silverlight-app";
                            break;
                    }
                }

                return mimeType;
            }
            catch (SecurityException)
            {
                // the above code performs a privileged operation
                // if we don't have correct privilege fall back to 
                // a simpler method
            }

            switch (extension)
            {
                case ".docx":
                case ".doc":
                    mimeType = "application/msword";
                    break;

                case ".jpg":
                case ".jpeg":
                    mimeType = "image/jpeg";
                    break;

                case ".png":
                    mimeType = "image/png";
                    break;

            }

            return mimeType;
        }
    }
}
